//
//  PlayerViewController.m
//  老司机发车了
//
//  Created by SuperNing on 16/8/9.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "PlayerViewController.h"
#import "MusicManager.h"
#import "PlayerModel.h"
#import "LrcTool.h"
#import "LrcLine.h"
#import "MusicDataBase.h"

@interface PlayerViewController ()<UITableViewDelegate,UITableViewDataSource,MusicPlayerDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *playerScrollView;

@property (weak, nonatomic) IBOutlet UILabel *currentTime;

@property (weak, nonatomic) IBOutlet UILabel *allTime;

@property (weak, nonatomic) IBOutlet UISlider *sliderProgress;

@property (nonatomic, strong)UIImageView *imgView;

@property (nonatomic, strong) UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *centerBtn;

@property (nonatomic, strong) UILabel *singerLabel;

@property (nonatomic, strong) UILabel *songLabel;

@property (nonatomic, strong) NSArray *lrcArray;
@end

@implementation PlayerViewController

- (NSArray *)lrcArray{
    
    if (!_lrcArray) {
        _lrcArray = [[NSArray alloc] init];
    }
    return _lrcArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    self.view.backgroundColor = [UIColor blackColor];
    [self addNavgiationItem];
    [self layoutUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataWithModel) name:@"loadData" object:nil];
    [MusicManager shareMusicManager].playStyle = 0;
    
    if (!self.songID) {
        
        [[MusicManager shareMusicManager] loadSongInfo];

    }else{
        [self reloadDataWithModel];
        _currentTime.text = [self setTime:[MusicManager shareMusicManager].tempProgress];
        _sliderProgress.value = [MusicManager shareMusicManager].tempProgress;
        if ([MusicManager shareMusicManager].isPlay == NO) {
            [_centerBtn setImage:[UIImage imageNamed:@"player_play@2x"] forState:UIControlStateNormal];
        }
        
    }
    

    [MusicManager shareMusicManager].delegate = self;
    
    [_sliderProgress addTarget:self action:@selector(sliderSeekToTime:) forControlEvents:UIControlEventValueChanged];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self addLrc];
    
}



- (void)addNavgiationItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"下"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_playerScrollView addSubview:leftBtn];
}
- (void)btnAction{
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)playStyleAction:(id)sender {
    
    UIButton *btn = sender;
    NSArray *imgNameArray = @[@"bt_playpagen_control_round-all_normal",@"bt_playpagen_control_round-one_normal",@"bt_playpagen_control_random_normal",@"bt_playpagen_control_order_normal"];
    if ([MusicManager shareMusicManager].playStyle == 3) {
        [MusicManager shareMusicManager].playStyle = 0;
    }else{
        [MusicManager shareMusicManager].playStyle++;
    }
    [btn setImage:[UIImage imageNamed:imgNameArray[[MusicManager shareMusicManager].playStyle]] forState:UIControlStateNormal];
    
}

- (IBAction)previousAction:(id)sender {
    
    
    [[MusicManager shareMusicManager] previousMusic];
    [[MusicManager shareMusicManager] loadSongInfo];
    [_centerBtn setImage:[UIImage imageNamed:@"player_pause@2x"] forState:UIControlStateNormal];
}

- (IBAction)playAndPauseAction:(id)sender {
 
    UIButton *btn = sender;
    if ([MusicManager shareMusicManager].isPlay == YES) {
        [btn setImage:[UIImage imageNamed:@"player_play@2x"] forState:UIControlStateNormal];
        [[MusicManager shareMusicManager] stopPlayMusic];
    }else{
        [btn setImage:[UIImage imageNamed:@"player_pause@2x"] forState:UIControlStateNormal];
        [MusicManager shareMusicManager].isPlay = YES;
        [[MusicManager shareMusicManager] startPlayMusic];
    }
}

- (IBAction)nextAction:(id)sender {
    
    [[MusicManager shareMusicManager] nextMusic];
    [[MusicManager shareMusicManager] loadSongInfo];
    [_centerBtn setImage:[UIImage imageNamed:@"player_pause@2x"] forState:UIControlStateNormal];
}

- (IBAction)downLoadAction:(id)sender {
    
    [[MusicManager shareMusicManager] downloadMusic];
    
}

- (void)sliderSeekToTime:(UISlider *)slider{
    
    [[MusicManager shareMusicManager] seekToTime:slider.value];
}

- (void)addLrc{
    
    PlayerModel *playmodel = [MusicManager shareMusicManager].playerModel;
    self.lrcArray = [LrcTool lrcToolWithLrcNameTwo:playmodel.lrclink];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        _singerLabel.text = playmodel.author;
        _songLabel.text = playmodel.title;
        
        _allTime.text = [self setTime:(float)playmodel.file_duration];
        
        [self.tableView reloadData];
        
    });
    

}

- (void)reloadDataWithModel{
    
    PlayerModel *playmodel = [MusicManager shareMusicManager].playerModel;
    [_imgView sd_setImageWithURL:[NSURL URLWithString:playmodel.pic_radio] placeholderImage:[UIImage imageNamed:@"wolf"]];
    _songID = playmodel.song_id;
    
    _sliderProgress.maximumValue = (float)playmodel.file_duration;
    
    [self addLrc];
    
}
- (NSString *)setTime:(float)progress{
    int min = (int)progress / 60;
    
    int sec = (int)progress % 60;
    
    NSString *timeStr = [NSString stringWithFormat:@"%02d:%02d", min,sec];
    return timeStr;
}


- (void)layoutUI{
    
    _playerScrollView.contentSize = CGSizeMake(screenWidth * 2, 0);
    
    CGFloat width = screenWidth * 0.65;
    CGFloat orginX = (screenWidth - width) / 2;
    CGFloat orginY = (_playerScrollView.ownHeight - 64 - width) / 2 + 64;
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(orginX, orginY, width, width)];
    _imgView.layer.cornerRadius = width / 2.f;
    _imgView.layer.masksToBounds = YES;
    [_playerScrollView addSubview:_imgView];
    
    _singerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 94, 200, 25)];
    _singerLabel.centerX = screenWidth / 2;
    _singerLabel.textAlignment = NSTextAlignmentCenter;
    _songLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _singerLabel.endY + 10, 200, 25)];
    _songLabel.centerX = screenWidth / 2;
    _songLabel.textAlignment = NSTextAlignmentCenter;
    _singerLabel.textColor = [UIColor whiteColor];
    _songLabel.textColor = [UIColor whiteColor];

//    _singerLabel.backgroundColor = [UIColor clearColor];
//    _songLabel.backgroundColor = [UIColor clearColor];
    [_playerScrollView addSubview:_singerLabel];
    [_playerScrollView addSubview:_songLabel];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(screenWidth, 0, screenWidth, _playerScrollView.ownHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_playerScrollView addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.lrcArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    LrcLine *lrcline = self.lrcArray[indexPath.row];
    cell.textLabel.text = lrcline.text;

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)autoRefreshProgress:(float)progress{
    
    _currentTime.text = [self setTime:progress];
    
    [MusicManager shareMusicManager].tempProgress = progress;
    
    _imgView.transform = CGAffineTransformRotate(_imgView.transform, 0.1);
    _sliderProgress.value = progress;
    
}



@end



